/**
 * 
 * @authors 冰剑
 * @date    2017-09-13 16:11:03
 * @Desc   Vuex 的 mutations
 */

import constantRouterMap from '@/router/route'
import storage from '@/utils/storage'
import CONFIG from '@/config/config'
import { tools } from '@/utils/tools'

export default {
  SET_USERINFO(state, payload) {
    state.user = payload
    if (payload) {
      storage.setItem(CONFIG.USERINFO, JSON.stringify(payload))
    } else {
      state.token = null
      storage.removeItem(CONFIG.TOKEN)
      tools.setCookie(CONFIG.COOKIE_NAME,0,-1)
      storage.removeItem(CONFIG.USERINFO)
    }
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_ROUTERS: (state, routers) => {
    state.addRouters = routers
    state.routers = constantRouterMap.concat(routers)
  },
}